{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import time\n",
    "import base64\n",
    "import hashlib\n",
    "import pandas as pd\n",
    "\n",
    "# 讯飞开放平台试用\n",
    "url = \"https://webapi.xfyun.cn/v1/service/v1/ocr/general\"\n",
    "APPID = \"4ef15fd6\"\n",
    "APIKey = \"5308340426539d781b76e739326af810\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 参数\n",
    "curTime = str(int(time.time()))\n",
    "param = {\"language\": \"cn|en\", \"location\": \"false\"}\n",
    "param = json.dumps(param)\n",
    "paramBase64 = base64.b64encode(param.encode('utf-8'))\n",
    "# 获取一个md5加密算法对象\n",
    "# 令牌，计算方法：MD5(APIKey + X-CurTime + X-Param)，三个值拼接的字符串，进行MD5哈希计算（32位小写）\n",
    "md = hashlib.md5()\n",
    "md0 = APIKey + curTime + str(paramBase64,'utf-8')\n",
    "md.update(md0.encode('utf-8'))\n",
    "checkSum = md.hexdigest()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [],
   "source": [
    "header = {\n",
    "    'X-Appid': APPID,\n",
    "    'X-CurTime': curTime,\n",
    "    'X-Param': paramBase64,\n",
    "    'X-CheckSum': checkSum,\n",
    "    'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(r\"E:\\学习\\作业\\Python数据挖掘\\Untitled Folder\\Verification_Code.png\", 'rb') as f:\n",
    "    Verification_Code = f.read()\n",
    "    \n",
    "data_base64 = str(base64.b64encode(Verification_Code), 'utf-8')\n",
    "data = {'image': data_base64}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(url, data, headers=header)\n",
    "VC = response.json()['data']['block'][0]['line'][0]['word'][0]['content']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ABCD'"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "VC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
